package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect1;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTask;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskClassRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.workcorrect1.HomeworkCorrect1Service;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

/**
 * 申请批阅
 */
@CrossOrigin
@RestController
public class Workcorrect1StuUpdController extends YxyApiBaseController {

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    HomeworkTaskClassRepository homeworkTaskClassRepository;

    @Autowired
    HomeworkCorrect1Service homeworkCorrect1Service;

    @Autowired
    YxUserPointsUtilV3 yxUserPointsUtilV3;
    
    @Autowired
    HomeworkTaskRepository homeworkTaskRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workcorrect1/upd.v1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // 参数 Id
        if (reqModel.homeworkTaskReportQuesId == null || reqModel.homeworkTaskReportQuesId.equals("")) {
            Assert.isTrue(false, "未找到学生相关通知作业，请重试！");
        }
        // 获取用户指定的答题
        HomeworkTaskReportQues homeworkTaskReportQues = homeworkTaskReportQuesRepository
                .findOne(reqModel.homeworkTaskReportQuesId);
        // 判断是否存在数据
        if (homeworkTaskReportQues == null) {
            Assert.isTrue(false, "未找到学生相关通知作业，请重试！");
        } else {
            // 只有试题类型为填空题或者简单题才对知识点操作
            if (reqModel.questionType.equals("3") || reqModel.questionType.equals("4")) {
                List<Integer> wrongKnowIdLst = new ArrayList<Integer>();
                List<String> wrongKnowNameLst = new ArrayList<>();
                List<Integer> exactnessKnowIdLst = new ArrayList<Integer>();
                List<String> exactnessKnowNameLst = new ArrayList<>();

                List<wrongKnow> wrongKnows = reqModel.wrongKnows;
                List<exactnessKnow> exactnessKnows = reqModel.exactnessKnows;

                for (exactnessKnow ex : exactnessKnows) {
                    exactnessKnowIdLst.add(ex.id);
                    exactnessKnowNameLst.add(ex.name);
                }
                for (wrongKnow wr : wrongKnows) {
                    wrongKnowIdLst.add(wr.id);
                    wrongKnowNameLst.add(wr.name);
                }

                homeworkTaskReportQues.setWrongKnowIdLst(wrongKnowIdLst);
                homeworkTaskReportQues.setWrongKnowNameLst(wrongKnowNameLst);
                homeworkTaskReportQues.setExactnessKnowIdLst(exactnessKnowIdLst);
                homeworkTaskReportQues.setExactnessKnowNameLst(exactnessKnowNameLst);
            }
            // 教师增加积分
            if (homeworkTaskReportQues.getMark() == null) {
                try {
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                yxUserPointsUtilV3.readHomework(getProfile(request, response).getId());
                            } catch (Exception e) {
                            }
                        }
                    });
                    thread.run();
                } catch (Exception e) {
                }
            }
            // 教师对学生的批阅
            homeworkTaskReportQues.setMark(reqModel.score);
            homeworkTaskReportQuesRepository.save(homeworkTaskReportQues);
            // 修改状态
            homeworkCorrect1Service.correct(reqModel.taskId, reqModel.classId, homeworkTaskReportQues.getStudentId());
        }
        // 获取状态
        HomeworkTaskReport homeworkTaskReport = homeworkTaskReportRepository.findTop1ByTaskIdAndClassIdAndStudentId(
                reqModel.taskId, reqModel.classId, homeworkTaskReportQues.getStudentId());
        respModel.statusOfFinished = homeworkTaskReport.getStatusOfFinished();
        // 获取当前是试题教师的评分
        HomeworkTaskReportQues reportQues = homeworkTaskReportQuesRepository.findOne(reqModel.homeworkTaskReportQuesId);
        // 获取本次任务
        HomeworkTask homeworkTask = homeworkTaskRepository.findById(reqModel.taskId);
        respModel.mark = reportQues.getMark();
        try {
			respModel.isLargeTime = (new Date()).getTime() > (homeworkTask.getPredictDate().getTime());
		} catch (Exception e) {
			respModel.isLargeTime = false;
		}
        return respModel;
    }

    /** 请求参数 **/
    protected static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // homeworkTaskReportQues Id
        public String homeworkTaskReportQuesId;
        // 班级Id
        public String classId;
        // 试题类型
        public String questionType;
        // 分数
        public Integer score;
        // 勾选错误知识点
        public List<wrongKnow> wrongKnows;
        // 正确知识点
        public List<exactnessKnow> exactnessKnows;
    }

    // 勾选错误知识点
    protected static class wrongKnow {
        public Integer id;
        public String name;
    }

    // 勾选正确知识点
    protected static class exactnessKnow {
        public Integer id;
        public String name;
    }

    /** 响应 **/
    protected static class RespModel extends YxyApiRespModel {
        // 作业完成情况:-1-逾期未提交,0-未做,1-完成 ,2-已批阅,3-答题中
        public Integer statusOfFinished = 1;
        // 当前作答教师评分
        public Integer mark;
        // 当前时间是否大于任务截止时间
        public Boolean isLargeTime = false;
    }
}
